package crp.core.portal.domain.services;

import com.qwlabs.lang.C2;
import crp.core.analysis.app.ViewFlowService;
import crp.core.course.domain.models.CourseEntity;
import crp.core.portal.domain.models.PortalSectionCourseLoadContext;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import java.util.List;
import java.util.Map;

@ApplicationScoped
public class PortalSectionCourseViewsLoader implements PortalSectionCourseLoader {
    private final ViewFlowService viewFlowService;

    @Inject
    public PortalSectionCourseViewsLoader(ViewFlowService viewFlowService) {
        this.viewFlowService = viewFlowService;
    }

    @Override
    public Map<String, List<CourseEntity>> load(PortalSectionCourseLoadContext context) {
        List<CourseEntity> courses = viewFlowService.searcher().topViewsPublicCourses(context.getLimit());
        return C2.map(context.getSections(), PortalSectionCourseLoadContext.SectionWithConfig::getSectionId, section -> courses);
    }
}
